// ExtractorApi.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "HtmlPage.h"

#include <windows.h>
#include <mshtml.h>
#include <fstream>
#include <time.h>


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// The one and only application object

CWinApp theApp;



typedef vector<string> strVecFilesContent;
typedef vector<string> strVecFilesNames;

int SearchDirectory(strVecFilesNames &refvecFiles,
                    const string   &refcstrRootDirectory,
                    const string   &refcstrExtension)
{
	string          strFilePath;             // Filepath
	string          strPattern;              // Pattern
	HANDLE          hFile;                   // Handle to file
	WIN32_FIND_DATA FileInformation;         // File information
	int i=0;

	strPattern = refcstrRootDirectory + "\\*." + refcstrExtension;

	hFile = ::FindFirstFile(strPattern.c_str(), &FileInformation);
	if( hFile != INVALID_HANDLE_VALUE )
	{
	do
	{
	  if(FileInformation.cFileName[0] != '.')
	  {
		strFilePath.erase();
		strFilePath = refcstrRootDirectory + "\\" + FileInformation.cFileName;

		if(FileInformation.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
		{
		  // Search subdirectory
		  int iRC = SearchDirectory(refvecFiles, strFilePath, refcstrExtension);
		  if(iRC)
			return iRC;
		}
		else
		  // Save filename			
		  refvecFiles.push_back( strFilePath );		
	  }
	} while(::FindNextFile(hFile, &FileInformation) == TRUE);
	// Close handle
	::FindClose(hFile);

	DWORD dwError = ::GetLastError();
	if(dwError != ERROR_NO_MORE_FILES)
	  return dwError;
	}

	return 0;
}//SearchDirectory

void SavePagesAndKeyPhrase(string DirPath , HtmlPageVector objHPVec , KeyPhraseMap objKPMap , int NumOfNZTerms)
{
	ofstream KPhraseFile, PagesFile, FilesNameFile;
	HtmlPageVector::iterator posHPVec;
	KeyPhraseMap::iterator posKPM;
	FeatureItemVector::iterator posFIVec;
	FeatureItemVector objFIvec;
	string KPFilePath( DirPath ), PagesFilePath( DirPath ), FilesNamesPath( DirPath );
	PagesFilePath += "PagesMatrix.txt";
	KPFilePath += "KeyPhrases.txt";
	FilesNamesPath += "FilesNames.txt";
	KPhraseFile.open( KPFilePath.c_str() , ios::out );
	PagesFile.open( PagesFilePath.c_str() , ios::out );
	FilesNameFile.open( FilesNamesPath.c_str() , ios::out );
	KPhraseFile<<objKPMap.size();
	//KPhraseFile<<"index; "<<"KeyPhrase; "<<"Frequency;\n";
	for ( posKPM = objKPMap.begin() ; posKPM != objKPMap.end() ; posKPM++ )
		KPhraseFile << posKPM->second.GetIndex() << "; " << posKPM->first.c_str() << "; " << posKPM->second.GetFrequency()<<endl;
	KPhraseFile.close();
	PagesFile<<objHPVec.size()<<" "<<objKPMap.size()<<" "<<NumOfNZTerms<<endl;
	for ( posHPVec = objHPVec.begin() ; posHPVec != objHPVec.end() ; posHPVec++ )
	{
		FilesNameFile<<posHPVec->GetMyUrl().c_str()<<endl;
		objFIvec=posHPVec->GetFeatures();
		PagesFile<<objFIvec.size()<<" ";
		for ( posFIVec = objFIvec.begin() ; posFIVec != objFIvec.end() ; posFIVec++ )
			PagesFile<<posFIVec->GetIndex()<<" "<<posFIVec->GetWeight()<<" "<<posFIVec->GetFrequency()<<" ";
		PagesFile<<endl;
	}
	PagesFile.close();
	FilesNameFile.close();
}//SavePagesAndKeyPhrase


string ReadFile(string FilePath)
{
	FILE *fp;
	string Stream;
	char w[2];
	w[1]='\0';
	fp=fopen( FilePath.c_str() , "r" );
	while( (fscanf(fp,"%c",&w[0])) != EOF )
		Stream.append(w,1);
	fclose( fp );
	return Stream;
}//ReadFile




int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
	int nRetCode = 0 , iRC , NumOfNZTerms=0;
	strVecFilesNames FilesNames;
	strVecFilesContent FilesContent;
	HtmlPageVector AllPagesVector;
	HtmlPage *pHtmlPage=NULL;
	char DirPath[300];
	string strFileContent;
	DirPath[0]='\0';
	strVecFilesContent::iterator posFilesContent;
	strVecFilesNames::iterator posFilesNames;
	KeyPhraseCollection MyKeyPhraseCollection;
	//MyKeyPhraseCollection.ReadKeyPhrasesFromFile( "c:\\KP.txt" );
	// initialize MFC and print and error on failure
	if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
	{
		// TODO: change error code to suit your needs
		cerr << _T("Fatal Error: MFC initialization failed") << endl;
		nRetCode = 1;
	}
	else
	{
		/*cout<<"please enter the web page's directory path\n";
		cin>>DirPath;*/
		void *StopMemory;
		int ErrorCode;
		char *ErrorMessage=NULL;
		ErrorCode = ExtrCreateStopMemory(&StopMemory);
		if (ErrorCode > 0)
		{
			ExtrGetErrorMessage(ErrorCode, &ErrorMessage);
			return false ;
		}
		strcpy( DirPath , "E:\\ThesisData\\Files" );
		iRC = SearchDirectory(FilesNames, DirPath, "*" );//read htm files
		cout<<endl<<FilesNames.size()<<endl;
		for ( posFilesNames = FilesNames.begin() ; posFilesNames != FilesNames.end() ; posFilesNames++ )
		{
			pHtmlPage=new HtmlPage( *posFilesNames );
			cout<<nRetCode++<<" ";
			strFileContent.assign( ReadFile(posFilesNames->c_str()) );
			if ( pHtmlPage->GenerateTermsList( (char*) strFileContent.c_str() , StopMemory , MyKeyPhraseCollection ) ) 
			{
				AllPagesVector.push_back( *pHtmlPage );			
				NumOfNZTerms+=pHtmlPage->GetNumOfIndexs();
			}
			delete pHtmlPage;
			strFileContent.erase();
			/*FilesContent.push_back( ReadFile( posFilesNames->c_str() ) );
			if ( FilesContent.size()%1000 == 0 )
				cout<<FilesContent.size();*/
		}
		nRetCode=0;
	/*	for ( posFilesContent = FilesContent.begin(),posFilesNames = FilesNames.begin() ; posFilesContent != FilesContent.end() ; posFilesContent++, posFilesNames++  )
		{
			pHtmlPage=new HtmlPage( *posFilesNames );
			cout<<nRetCode++<<" ";			
			if ( pHtmlPage->GenerateTermsList( (char*) posFilesContent->c_str() , MyKeyPhraseCollection ) ) 
				AllPagesVector.push_back( *pHtmlPage );			
			delete pHtmlPage;			
		}*/
		cout<<"\nFinito La comed";
		ExtrClearStopMemory(StopMemory);
		nRetCode = 1;
		if ( nRetCode == 1 )
			SavePagesAndKeyPhrase("output\\" , AllPagesVector , MyKeyPhraseCollection.GetKeyPhraseMap() , NumOfNZTerms);
	}
	return nRetCode;
}


